package net.htjs.juc.chapter04;

import java.util.ArrayList;

public class TestThreadSafeSubClass {

    static final int THREAD_NUMBER = 2;
    static final int LOOP_NUMBER = 200;

    public static void main(String[] args) {
        ThreadSafeSubClass test = new ThreadSafeSubClass();
        for (int i = 0; i < THREAD_NUMBER; i++) {
            new Thread(() -> {
                test.method1(LOOP_NUMBER);
            }, "Thread-" + (i +1)).start();
        }
    }

    static class ThreadSafe {
        public void method1(int loopNumber) {
            ArrayList<String> list = new ArrayList<>();
            for(int i = 0; i < loopNumber; i++) {
                //临界区，会产生竟态条件
                method2(list);
                method3(list);
            }
        }

        public void method2(ArrayList<String> list) {
            list.add("1");
        }

        public void method3(ArrayList<String> list) {
            list.remove(0);
        }

    }

    /**
     * "D:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.2\lib\idea_rt.jar=64158:D:\Program Files\JetBrains\IntelliJ IDEA 2019.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\IDEAProject\juc-heima\target\classes;D:\repository\org\springframework\boot\spring-boot-starter-web\2.5.6\spring-boot-starter-web-2.5.6.jar;D:\repository\org\springframework\boot\spring-boot-starter\2.5.6\spring-boot-starter-2.5.6.jar;D:\repository\org\springframework\boot\spring-boot\2.5.6\spring-boot-2.5.6.jar;D:\repository\org\springframework\boot\spring-boot-autoconfigure\2.5.6\spring-boot-autoconfigure-2.5.6.jar;D:\repository\org\springframework\boot\spring-boot-starter-logging\2.5.6\spring-boot-starter-logging-2.5.6.jar;D:\repository\ch\qos\logback\logback-classic\1.2.6\logback-classic-1.2.6.jar;D:\repository\ch\qos\logback\logback-core\1.2.6\logback-core-1.2.6.jar;D:\repository\org\apache\logging\log4j\log4j-to-slf4j\2.14.1\log4j-to-slf4j-2.14.1.jar;D:\repository\org\apache\logging\log4j\log4j-api\2.14.1\log4j-api-2.14.1.jar;D:\repository\org\slf4j\jul-to-slf4j\1.7.32\jul-to-slf4j-1.7.32.jar;D:\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\repository\org\yaml\snakeyaml\1.28\snakeyaml-1.28.jar;D:\repository\org\springframework\boot\spring-boot-starter-json\2.5.6\spring-boot-starter-json-2.5.6.jar;D:\repository\com\fasterxml\jackson\core\jackson-databind\2.12.5\jackson-databind-2.12.5.jar;D:\repository\com\fasterxml\jackson\core\jackson-annotations\2.12.5\jackson-annotations-2.12.5.jar;D:\repository\com\fasterxml\jackson\core\jackson-core\2.12.5\jackson-core-2.12.5.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.12.5\jackson-datatype-jdk8-2.12.5.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.12.5\jackson-datatype-jsr310-2.12.5.jar;D:\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.12.5\jackson-module-parameter-names-2.12.5.jar;D:\repository\org\springframework\boot\spring-boot-starter-tomcat\2.5.6\spring-boot-starter-tomcat-2.5.6.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.54\tomcat-embed-core-9.0.54.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.54\tomcat-embed-el-9.0.54.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.54\tomcat-embed-websocket-9.0.54.jar;D:\repository\org\springframework\spring-web\5.3.12\spring-web-5.3.12.jar;D:\repository\org\springframework\spring-beans\5.3.12\spring-beans-5.3.12.jar;D:\repository\org\springframework\spring-webmvc\5.3.12\spring-webmvc-5.3.12.jar;D:\repository\org\springframework\spring-aop\5.3.12\spring-aop-5.3.12.jar;D:\repository\org\springframework\spring-context\5.3.12\spring-context-5.3.12.jar;D:\repository\org\springframework\spring-expression\5.3.12\spring-expression-5.3.12.jar;D:\repository\org\projectlombok\lombok\1.18.20\lombok-1.18.20.jar;D:\repository\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar;D:\repository\org\springframework\spring-core\5.3.12\spring-core-5.3.12.jar;D:\repository\org\springframework\spring-jcl\5.3.12\spring-jcl-5.3.12.jar" net.htjs.juc.chapter4.TestThreadUnsafe
     * Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
     * 	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
     * 	at java.util.ArrayList.remove(ArrayList.java:496)
     * 	at net.htjs.juc.chapter4.TestThreadUnsafe$ThreadUnsafe.method3(TestThreadUnsafe.java:39)
     * 	at net.htjs.juc.chapter4.TestThreadUnsafe$ThreadUnsafe.method1(TestThreadUnsafe.java:29)
     * 	at net.htjs.juc.chapter4.TestThreadUnsafe.lambda$main$0(TestThreadUnsafe.java:15)
     * 	at java.lang.Thread.run(Thread.java:748)
     *
     * Process finished with exit code 0
     * 非线程安全，可以再父方法上加上final阻止子类重写
     */
    static class ThreadSafeSubClass extends ThreadSafe {
        @Override
        public void method3(ArrayList<String> list) {
            new Thread(() -> {
                list.remove(0);
            }).start();
        }
    }

}
